package com.loong.uiframetest.QMUI;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.loong.uiframetest.R;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

public class QmuidialogTest extends AppCompatActivity implements View.OnClickListener {
    private TextView tvShowMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qmuidialog_test);
        init();
    }

    private void init() {
        tvShowMsg = findViewById(R.id.tv_showmsg);
        //普通消息对话框
        findViewById(R.id.btn_dialog_1).setOnClickListener(this);
        //带CheckBox的消息确认框
        findViewById(R.id.btn_dialog_2).setOnClickListener(this);
        //带输入框的对话框
        findViewById(R.id.btn_dialog_3).setOnClickListener(this);
        //菜单类型的对话框
        findViewById(R.id.btn_dialog_4).setOnClickListener(this);
        //单选类型的对话框
        findViewById(R.id.btn_dialog_5).setOnClickListener(this);
        //多选类型的对话框
        findViewById(R.id.btn_dialog_6).setOnClickListener(this);
    }

    /**
     * 按钮点击监听事件
     *
     * @param v 视图
     */
    @Override
    public void onClick(View v) {
        //菜单选项
        final String[] items = new String[]{"选项一","选项二","选项三"};
        switch (v.getId()) {
            case R.id.btn_dialog_1://普通的消息对话框
                new QMUIDialog.MessageDialogBuilder(this)
                        .setTitle("MessageDialog测试")
                        .setMessage("确定要发送吗？")
                        .addAction("取消", (dialog, index) -> {
                            dialog.dismiss();//销毁当前对话框
                        })
                        .addAction("确定", (dialog, index) -> {
                            tvShowMsg.setText("MessageDialog确认点击");
                            dialog.dismiss();//销毁按钮
                        })
                        .show();
                break;
            case R.id.btn_dialog_2:
                new QMUIDialog.CheckBoxMessageDialogBuilder(this)
                        .setTitle("退出后是否保存信息")
                        .setMessage("保存信息")
                        .setChecked(true)
                        .addAction("取消", (dialog, index) -> {
                            dialog.dismiss();
                        })
                        .addAction("退出", (dialog, index) -> {
                            tvShowMsg.setText("CheckBoxMessageDialog:");
                            dialog.dismiss();
                        }).show();
                break;
            case R.id.btn_dialog_3:

                final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(this);
                builder.setPlaceholder("请输入姓名")
                        .setTitle("标题党")
                        .setDefaultText("Loong")
                        .setInputType(InputType.TYPE_CLASS_TEXT)
                        .addAction("取消", (dialog, index) -> {
                            dialog.dismiss();
                        })
                        .addAction("确定", (dialog, index) -> {
                            CharSequence text = builder.getEditText().getText();
                            if (text != null && text.length() > 0) {
                                tvShowMsg.setText("姓名:" + text);
                                dialog.dismiss();//销毁对话框
                            } else {
                                Toast.makeText(this, "请输入您的姓名", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
                break;
            case R.id.btn_dialog_4:
                //菜单项
                new QMUIDialog.MenuDialogBuilder(this)
                        .addItems(items, (dialog, which) -> {
                            tvShowMsg.setText("您选择了"+items[which]);
                            dialog.dismiss();
                        }).show();
                break;
            case R.id.btn_dialog_5:
                //选项
                final QMUIDialog.CheckableDialogBuilder cdBuilder = new QMUIDialog.CheckableDialogBuilder(this);
                cdBuilder.setTitle("请选择")
                        .setCheckedIndex(1)//选中的索引
                        .addItems(items,(dialog,which)->{
                            cdBuilder.setCheckedIndex(which);//选中当前
                        })
                        .addAction("确定",(dialog,index)->{
                            tvShowMsg.setText("您选择了"+items[cdBuilder.getCheckedIndex()]);
                            dialog.dismiss();
                        }).show();
                break;
            case R.id.btn_dialog_6:
                QMUIDialog.MultiCheckableDialogBuilder mcdBuilder = new QMUIDialog.MultiCheckableDialogBuilder(this);
                mcdBuilder.setTitle("请选择若干个选项")
                        .setCheckedItems(new int[]{1})//设置默认选中项
                        .addItems(items, (dialog, which) -> {

                        })
                        .addAction("取消", (dialog, index) -> {
                            dialog.dismiss();
                        })
                        .addAction("提交", (dialog, index) -> {
                            StringBuilder result = new StringBuilder("您选择了");
                            for (int i : mcdBuilder.getCheckedItemIndexes()) {
                                result.append(i)
                                        .append(";");
                            }
                            tvShowMsg.setText(result);
                            dialog.dismiss();
                        }).show();

                break;
            default:
                break;
        }
    }
}
